window.onload = () => {

    let username = document.getElementById("username")
    let id = document.getElementById("id")

    let btn = document.querySelector("button")
    btn.addEventListener("click", (e) => {
        e.preventDefault()

        let name = username.value
        let id = id.value
            // XMLHttpRequest
        let xhr = new XMLHttpRequest()
            //1、建立连接
        xhr.open("POST", "http://172.19.11.61:8080/login" + "?username" + name + "&id=" + id, true)
            //2、异步请求的状态监听
        xhr.onreadystatechange = function() {
            //异步请求的状态 readyState：
            //0：请求已经创建，但是还没有调用open
            //1：调用了open，但是还没有发送
            //2：服务器返回响应头
            //3：正在读取响应体
            //4：请求完成，响应可用
            if (xhr.readyState === 4 && xhr.status === 200) {
                //4、得到响应
                console.log("请求成功")
            }
        }

        xhr.send()
            //Promise
            //get

        fetch('http://172.19.11.61:8080/login?name=' + name + '&id=' + id)
            .then(response => {
                if (response.ok) {
                    console.log("请求成功")
                } else {
                    console.log("请求失败，状态码：", response.status)
                }
                return response.text()
            })
            .catch(error => {
                console.log("请求出错：", error.message)
            })

        //post
        let stuData = {
            name: name,
            id: id
        }
        fetch("http://172.19.11.61:8080/login", {
                method: "POST",
                headers: {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify(stuData)
            })
            .then(response => {
                if (response.ok) {
                    console.log("请求成功")
                } else {
                    console.log("请求失败，状态码：", response.status)
                }
                return response.text()
            })
            .catch(error => {
                console.log("请求出错：", error.message)
            })


    })
}